﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using WCFMINERP.COM;
using WCFMINERP.DAL;
using WCFMINERP.DAL.InnerFactory;
using WCFMINERP.InnerFactoryContracts.DataContracts;

namespace WCFMINERP.BLL.InnerFactory
{
    public class InnerFactoryBLL
    {
        private InnerFactoryDAL _dal = new InnerFactoryDAL();


        public List<ProduceView> NewGetProduceViewList(int inFactoryId)
        {
            List<ProduceView> ProduceViewLists = new List<ProduceView>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                int Status = (int)OutProcessRecordStatus.Fetched;
                int Status2 = (int)OutProcessRecordStatus2.Complete;
                int OrderStatus = (int)OrderInfoStatus.Doing;

                List<OutProcessRecord> outProcessRecords = db.OutProcessRecord.Where(p => p.orderInfo.status == OrderStatus && p.OutProcessShop.OutShopId == inFactoryId && p.Status != Status && p.Status2 != (int)Status2).ToList();

                foreach (OutProcessRecord list in outProcessRecords)
                {
                    if (!list.orderInfoReference.IsLoaded)
                    {
                        list.orderInfoReference.Load();
                    }
                    orderInfo product = list.orderInfo;
                    product.Tb_ProductReference.Load();
                    Tb_Product productType = product.Tb_Product;
                    //newView.Product = new OrderInfo_Info() { ProductId = product.OrderProductId, CompleteTime = product.CompleteTime, CtmName = product.CtmName, Height = product.Height, Width = product.Width, OrderProductCode = product.OrderProductCode, Remark = product.Remark, OrderProductName = product.OrderProductName, PageNum = product.PageNum, OrderPath = product.OrderPath };
                    //newView.ProductType = new Tb_Product_Info() { ProuctId = productType.ProuctId, ProductName = productType.ProductName };

                    if (!product.Tb_OrderBagReference.IsLoaded)
                    {
                        product.Tb_OrderBagReference.Load();
                    }
                    Tb_OrderBag orderBag = product.Tb_OrderBag;
                    orderBag.ShopReference.Load();
                    //newView.Order = new Tb_OrderBag_Info() { BagCode = orderBag.BagCode, BagName = orderBag.BagName, OrderBagId = orderBag.OrderBagId, ShopID = orderBag.Shop.ShopId, ShopName = orderBag.Shop.shopName, FetchTime = orderBag.FetchTime };
                    //newView.ProduceInfo = new OutProcessRecord_Info() { CreateTime = list.CreateTime, EndProductTime = list.EndProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.EndProductTime, StartProductTime = list.StartProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.StartProductTime, Status = list.Status, Status2 = list.Status2, OutRemark = list.OutRemark, Remark = list.Remark };
                    list.OutProcessShopReference.Load();
                    OutProcessShop shop = list.OutProcessShop;
                    //newView.Factory = new OutProcessShop_Info() { OutShopId = shop.OutShopId, OutShopName = shop.OutShopName };
                    ProduceView newView = new ProduceView()
                    {
                        CreateTime = list.CreateTime,
                        EndProductTime = list.EndProductTime,
                        CtmName = product.CtmName,
                        OrderPath = product.OrderPath,
                        ProductName = product.OrderProductName,
                        Remark = product.Remark + list.OutRemark,
                        OrderBarcode = orderBag.BagCode,
                        ProductId = product.OrderProductId,
                        FacthTime = orderBag.FetchTime,
                        FactoryId = shop.OutShopId,
                        FactoryName = shop.OutShopName,
                        IntStatus = list.Status,
                        IntStutas2 = list.Status2,
                        OrderId = orderBag.OrderBagId,
                        OrderName = orderBag.BagName,
                        OutProcessRecordId = list.OutProcessRecordId,
                        PageNum = product.PageNum,
                        StartProductTime = list.StartProductTime,
                        ProduceRemark = list.Remark,
                        ProductCode = product.OrderProductCode,
                        ProductNum = product.productNum,
                        ShopId = orderBag.Shop.ShopId,
                        ShopName = orderBag.Shop.shopName,
                        Size = product.Width + "X" + product.Height,
                        tb_ProductName = productType.ProductName,

                    };
                    switch (newView.IntStatus)
                    {
                        case (int)OutProcessRecordStatus.Outing:
                            {
                                switch (newView.IntStutas2)
                                {
                                    case (int)OutProcessRecordStatus2.Doing:
                                        newView.Status = "正在生产";
                                        break;
                                    case (int)OutProcessRecordStatus2.Error:
                                        newView.Status = "暂停生产";
                                        break;
                                    case (int)OutProcessRecordStatus2.Complete:
                                        newView.Status = "生产完成";
                                        break;
                                    default:
                                        newView.Status = "等待接收";
                                        break;
                                }
                            }
                            break;
                        case (int)OutProcessRecordStatus.InGrid:
                            {
                                newView.Status = "未打印(在格子中)";
                            }
                            break;
                        case (int)OutProcessRecordStatus.OutWaitFenDui:
                            {
                                newView.Status = "未打印(等待分堆)";
                            }

                            break;
                    }
                    productType.Tb_OutShop_ProductCategory.Load();
                    Tb_OutShop_ProductCategory pc = productType.Tb_OutShop_ProductCategory.Where(p => p.OutShopId == shop.OutShopId).FirstOrDefault();
                    if (pc != null)
                    {
                        newView.TimeSpan = pc.NeedTime;
                    }
                    else
                    {
                        newView.TimeSpan = 0;
                    }
                    if (list.StartProductTime != null)
                    {
                        newView.PreCompleteDateTime = ((DateTime)list.StartProductTime).AddHours(newView.TimeSpan);
                    }
                    else
                    {
                        newView.PreCompleteDateTime = DateTime.Now.AddHours(newView.TimeSpan);
                    }
                    ProduceViewLists.Add(newView);
                }
            }
            return ProduceViewLists;
        }


        /// <summary>
        /// 开始生产
        /// </summary>
        /// <param name="inUser"></param>
        /// <param name="inStrErr"></param>
        /// <param name="inoutRecordId"></param>
        public void StartProduce(int inUser, int inoutRecordId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessRecord outProcessRecord = db.OutProcessRecord.Where(p => p.OutProcessRecordId == inoutRecordId).FirstOrDefault();
                outProcessRecord.StartProductTime = DateTime.Now;
                outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Doing;
                outProcessRecord.OperateUser = inUser;
                db.SaveChanges();

            }
        }
        /// <summary>
        /// 结束生产
        /// </summary>
        /// <param name="inUser"></param>
        /// <param name="inStrErr"></param>
        /// <param name="inoutRecordId"></param>
        public void EndProduce(int inUser, int inoutRecordId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessRecord outProcessRecord = db.OutProcessRecord.Where(p => p.OutProcessRecordId == inoutRecordId).FirstOrDefault();
                outProcessRecord.EndProductTime = DateTime.Now;
                outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Complete;
                outProcessRecord.OperateUser = inUser;
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 恢复生产
        /// </summary>
        /// <param name="inUser"></param>
        /// <param name="inStrErr"></param>
        /// <param name="inoutRecordId"></param>
        public void RebootProduce(int inUser, int inoutRecordId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessRecord outProcessRecord = db.OutProcessRecord.Where(p => p.OutProcessRecordId == inoutRecordId).FirstOrDefault();
                outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Doing;
                outProcessRecord.OperateUser = inUser;
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 出错暂停生产
        /// </summary>
        /// <param name="inUser"></param>
        /// <param name="inoutRecordId"></param>
        /// <param name="inStrErr"></param>
        public void PauseProduce(int inUser, string inOutBarCode, string inStrErr)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessRecord outProcessRecord = db.OutProcessRecord.Where(p => p.orderInfo.OrderProductCode == inOutBarCode).FirstOrDefault();
                outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Error;
                outProcessRecord.OperateUser = inUser;
                outProcessRecord.Remark += inStrErr;
                db.SaveChanges();
            }
        }


        public OutProcessRecord_Info GetReCordByCode(string productBarCode)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessRecord outProcessRecord = db.OutProcessRecord.FirstOrDefault(o => o.orderInfo.OrderProductCode == productBarCode);
                if (outProcessRecord == null)
                {
                    return null;
                }
                OutProcessRecord_Info info = new OutProcessRecord_Info() { Status = outProcessRecord.Status, Status2 = outProcessRecord.Status2, OutProcessRecordId = outProcessRecord.OutProcessRecordId };
                return info;
            }
        }
        public OutProcessShop_Info GetOutShopById(int outShopId)
        {

            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessShop outProcessRecord = db.OutProcessShop.Where(p => p.OutShopId == outShopId).FirstOrDefault();
                if (outProcessRecord == null)
                {
                    return null;
                }
                OutProcessShop_Info info = new OutProcessShop_Info() { OutShopId = outProcessRecord.OutShopId, OutShopName = outProcessRecord.OutShopName };
                return info;
            }

        }
        public List<OutProcessShop_Info> GetAllInnerFactory()
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<OutProcessShop> lists = db.OutProcessShop.ToList();

                List<OutProcessShop_Info> outProcessShopInfos = new List<OutProcessShop_Info>();
                foreach (OutProcessShop shop in lists)
                {
                    OutProcessShop_Info info = new OutProcessShop_Info() { OutShopId = shop.OutShopId, OutShopName = shop.OutShopName };
                    outProcessShopInfos.Add(info);
                }
                return outProcessShopInfos;
            }
        }
        public DataTable GetPrintInfo(string inProductId)
        {
            return _dal.GetPrintInfo(inProductId);
        }
        public DataTable GetPrintInfoByPhotoBarcode(string photoBarcode)
        {
            string orderBarcode="";
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                orderInfo order = db.orderInfo.FirstOrDefault(p => p.photoInfo.Count(ph => ph.PhotoBarCode == photoBarcode) > 0);
                if (order != null)
                {
                    orderBarcode = order.OrderProductCode;
                }
            }
            if (!string.IsNullOrEmpty(orderBarcode))
            {
                return GetPrintInfo(orderBarcode);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 取得打印报表数据
        /// </summary>
        /// <param name="inGridId"></param>
        /// <param name="inProduct"></param>
        public DataSet GetReportdata(string inProduct, string inGridName)
        {
            DataSet ds = new DataSet();
            orderInfo product = null;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                if (inProduct.Substring(0, 1) == "2")
                {
                    product = Entities.orderInfo.FirstOrDefault(o => o.OrderProductCode == inProduct);
                }
                else
                {
                    photoInfo photo = Entities.photoInfo.FirstOrDefault(p => p.PhotoBarCode == inProduct);
                    if (photo == null)
                        return ds;
                    if (!photo.orderInfoReference.IsLoaded)
                    {
                        photo.orderInfoReference.Load();
                    }
                    product = photo.orderInfo;
                }

                if (product == null)
                {
                    return ds;
                }
                if (!product.Tb_OrderBagReference.IsLoaded)
                {
                    product.Tb_OrderBagReference.Load();
                }
                if (!product.Tb_OrderBag.ShopReference.IsLoaded)
                {
                    product.Tb_OrderBag.ShopReference.Load();
                }


                DataTable infoTable = CreateInfoTable();
                infoTable.TableName = "infoTable";
                DataRow rowInfo = infoTable.NewRow();
                rowInfo["BagCode"] = product.Tb_OrderBag.BagCode;
                rowInfo["BagName"] = product.Tb_OrderBag.BagName;
                rowInfo["shopName"] = product.Tb_OrderBag.Shop.shopName;
                rowInfo["OrderProductCode"] = "*" + product.OrderProductCode + "*";
                rowInfo["OrderProductName"] = product.OrderProductName;
                rowInfo["CtmName"] = product.CtmName;
                rowInfo["productNum"] = product.productNum;
                rowInfo["remark"] = product.Remark;
                rowInfo["GridName"] = inGridName;
                rowInfo["OrderTime"] = product.Tb_OrderBag.OrderTime;

                infoTable.Rows.Add(rowInfo);
                if (product.Priority >= 11)
                {
                    rowInfo["Priority"] = "特急件";
                }
                else if (product.Priority < 11 && product.Priority >= 6)
                {
                    rowInfo["Priority"] = "急件";
                }
                else
                {
                    rowInfo["Priority"] = "";
                }

                DataTable photoTable = CreatePhotoInfoTable();
                Dictionary<string, int> photos = new Dictionary<string, int>();
                //Dictionary<string, int> photosCM = new Dictionary<string, int>();
                if (!product.photoInfo.IsLoaded)
                {
                    product.photoInfo.Load();
                }
                foreach (photoInfo list in product.photoInfo)
                {
                    string photoSize = "" + Math.Round(list.Width, 2) + "×" + Math.Round(list.Height, 2) + "in(" + Math.Round(list.Width * 2.54, 2) + "×" + Math.Round(list.Height * 2.54, 2) + "cm)";
                    string photoSize1 = "" + Math.Round(list.Height, 2) + "×" + Math.Round(list.Width, 2) + "in(" + Math.Round(list.Height * 2.54, 2) + "×" + Math.Round(list.Width * 2.54, 2) + "cm)";

                    //string photoSizeCM = Math.Round(list.Width * 2.54, 2) + "×" + Math.Round(list.Height, 2);
                    if (photos.Keys.Contains(photoSize))
                    {
                        photos[photoSize] += 1;
                    }
                    else if (photos.Keys.Contains(photoSize1))
                    {
                        photos[photoSize1] += 1;
                    }
                    else
                    {
                        photos.Add(photoSize, 1);
                        //photosCM.Add(photoSizeCM, 1);
                    }
                }
                foreach (var list in photos)
                {
                    DataRow row = photoTable.NewRow();
                    row["Size"] = list.Key;
                    row["Num"] = list.Value;
                    photoTable.Rows.Add(row);

                }
                photoTable.TableName = "photoTable";

                if (!product.WorkOrderFlowList.IsLoaded)
                {
                    product.WorkOrderFlowList.Load();
                }
                //List<WorkOrderFlowList> flows = _dal.GetAllFlowByProductId(inProduct).OrderBy(p => p.WorkOrder).ToList();
                string strFlow = string.Empty;
                foreach (WorkOrderFlowList list in product.WorkOrderFlowList.OrderBy(p => p.WorkOrder))
                {
                    if (!list.tb_TechReference.IsLoaded)
                    {
                        list.tb_TechReference.Load();
                    }
                    strFlow = strFlow + list.tb_Tech.TechName + "->";
                }
                strFlow = strFlow.Trim("->".ToCharArray());
                DataTable flowTable = CreateTechFlow();
                //foreach (WorkOrderFlowList list in flows)
                //{
                //    if (!list.tb_TechReference.IsLoaded)
                //    {
                //        list.tb_TechReference.Load();
                //    }
                //    users user = _dal.GetUserById(list.UserId);
                //    DataRow row = flowTable.NewRow();
                //    row["TechName"] = list.tb_Tech.TechName;
                //    if (user != null)
                //    {
                //        row["UserName"] = user.UserName;
                //    }
                //    else
                //    {
                //        row["UserName"] = "";
                //    }
                //    flowTable.Rows.Add(row);
                //}
                DataRow rowt = flowTable.NewRow();
                rowt["TechName"] = strFlow;
                rowt["UserName"] = "0";
                flowTable.Rows.Add(rowt);
                flowTable.TableName = "flowTable";

                ds.Tables.Add(photoTable);
                ds.Tables.Add(infoTable);
                ds.Tables.Add(flowTable);
            }
            return ds;
        }

        /// <summary>
        ///  基本信息表
        /// </summary>
        /// <returns></returns>
        public DataTable CreateInfoTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BagCode", typeof(string));
            table.Columns.Add("BagName", typeof(string));
            table.Columns.Add("shopName", typeof(string));
            table.Columns.Add("OrderProductCode", typeof(string));
            table.Columns.Add("OrderProductName", typeof(string));
            table.Columns.Add("CtmName", typeof(string));
            table.Columns.Add("productNum", typeof(int));
            table.Columns.Add("remark", typeof(string));
            table.Columns.Add("GridName", typeof(string));
            table.Columns.Add("Priority", typeof(string));
            table.Columns.Add("OrderTime", typeof(string));
            return table;

        }

        /// <summary>
        /// 照片信息
        /// </summary>
        /// <returns></returns>
        public DataTable CreatePhotoInfoTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Size", typeof(string));
            //table.Columns.Add("Size1", typeof(string));
            table.Columns.Add("Num", typeof(int));
            return table;
        }

        /// <summary>
        /// 流程信息
        /// </summary>
        /// <returns></returns>
        public DataTable CreateTechFlow()
        {
            DataTable table = new DataTable();
            table.Columns.Add("TechName", typeof(string));
            table.Columns.Add("UserName", typeof(string));
            return table;
        }

        /// <summary>
        /// 检查产品是否存在
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public bool IsProductExist(string productCode)
        {
            bool flag = true;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                orderInfo oi = Entities.orderInfo.FirstOrDefault(o => o.OrderProductCode == productCode);
                if (oi == null)
                {
                    flag = false;
                }
            }

            return flag;
        }

        /// <summary>
        /// 检查照片是否存在
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public bool IsPhotoExist(string photoBarCode)
        {
            bool flag = true;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                photoInfo photo = Entities.photoInfo.FirstOrDefault(o => o.PhotoBarCode == photoBarCode);
                if (photo == null)
                {
                    flag = false;
                }
            }

            return flag;
        }


        /// <summary>
        /// 用户验证
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public UserInfo UserLogin(string loginName, string pwd)
        {
            UserInfo userInfo = null;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                users user = db.users.FirstOrDefault(u => u.LoginName == loginName && u.LoginPwd == pwd);
                
                if (user != null)
                {
                    userInfo = new UserInfo()
                    {
                        LoginName = user.LoginName,
                        Pwd = user.LoginPwd,
                        UserId = user.userID,
                        UserName = user.UserName

                    };

                    if (!user.tb_Tech.IsLoaded)
                    {
                        user.tb_Tech.Load();
                    }
                    
                    List<TechModel> model=new List<TechModel>();
                    foreach (tb_Tech tch in user.tb_Tech)
                    {
                        TechModel itm = new TechModel();
                        itm.TechId = tch.TechId;
                        itm.TechName = tch.TechName;
                        itm.TechType = tch.TechType;
                        itm.WokerOrder = tch.WokerOrder;

                        itm.IsNecessary = tch.IsNecessary;
                        itm.IsDesign = tch.IsDesign;

                        itm.FrmName = tch.FrmName;
                        itm.DirectoryName = tch.DirectoryName;
                        itm.Des = tch.Des;

                        model.Add(itm);
                    }     
                    userInfo.TechModelList = model;
                }
            }
            return userInfo;

        }


        /// <summary>
        /// 根据激活码找到对应委外商
        /// </summary>
        /// <param name="shacode"></param>
        /// <returns></returns>
        public OutProcessShop_Info GetOutShopByActiveCode(string shacode)
        {
            OutProcessShop_Info osInfo = null;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessShop os = db.OutProcessShop.FirstOrDefault(o => o.SHACODE == shacode);
                if (os != null)
                {
                    osInfo = new OutProcessShop_Info()
                    {
                        Addr = os.Addr,
                        City = os.City,
                        Code = os.Code,
                        EmailAddress = os.EmailAddress,
                        IsUse = os.IsUse,
                        Keeper = os.Keeper,
                        OutShopId = os.OutShopId,
                        OutShopName = os.OutShopName,
                        Remark = os.Remark,
                        Tel1 = os.Tel1,
                        Tel2 = os.Tel2,
                        Type = os.Type
                    };
                }
            }
            return osInfo;
        }
        /// <summary>
        /// 根据照片号获取产品信息
        /// 
        /// </summary>
        /// <param name="photoBarCode"></param>
        /// <returns></returns>
        public ProduceView GetProduceViewByPhotoCode(string photoBarCode)
        {
            ProduceView newView = new ProduceView();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                int Status = (int)OutProcessRecordStatus.Fetched;
                int Status2 = (int)OutProcessRecordStatus2.Complete;
                int OrderStatus = (int)OrderInfoStatus.Doing;
                orderInfo order = db.orderInfo.FirstOrDefault(p => p.photoInfo.Count(ph => ph.PhotoBarCode == photoBarCode) > 0);
                OutProcessRecord outProcessRecord = db.OutProcessRecord.FirstOrDefault(p => p.orderInfo.OrderProductId == order.OrderProductId);


                if (!outProcessRecord.orderInfoReference.IsLoaded)
                {
                    outProcessRecord.orderInfoReference.Load();
                }
                orderInfo product = outProcessRecord.orderInfo;
                product.Tb_ProductReference.Load();
                Tb_Product productType = product.Tb_Product;
                //newView.Product = new OrderInfo_Info() { ProductId = product.OrderProductId, CompleteTime = product.CompleteTime, CtmName = product.CtmName, Height = product.Height, Width = product.Width, OrderProductCode = product.OrderProductCode, Remark = product.Remark, OrderProductName = product.OrderProductName, PageNum = product.PageNum, OrderPath = product.OrderPath };
                //newView.ProductType = new Tb_Product_Info() { ProuctId = productType.ProuctId, ProductName = productType.ProductName };

                if (!product.Tb_OrderBagReference.IsLoaded)
                {
                    product.Tb_OrderBagReference.Load();
                }
                Tb_OrderBag orderBag = product.Tb_OrderBag;
                orderBag.ShopReference.Load();
                //newView.Order = new Tb_OrderBag_Info() { BagCode = orderBag.BagCode, BagName = orderBag.BagName, OrderBagId = orderBag.OrderBagId, ShopID = orderBag.Shop.ShopId, ShopName = orderBag.Shop.shopName, FetchTime = orderBag.FetchTime };
                //newView.ProduceInfo = new OutProcessRecord_Info() { CreateTime = list.CreateTime, EndProductTime = list.EndProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.EndProductTime, StartProductTime = list.StartProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.StartProductTime, Status = list.Status, Status2 = list.Status2, OutRemark = list.OutRemark, Remark = list.Remark };
                outProcessRecord.OutProcessShopReference.Load();
                OutProcessShop shop = outProcessRecord.OutProcessShop;
                //newView.Factory = new OutProcessShop_Info() { OutShopId = shop.OutShopId, OutShopName = shop.OutShopName };
                newView = new ProduceView()
               {
                   CreateTime = outProcessRecord.CreateTime,
                   EndProductTime = outProcessRecord.EndProductTime,
                   CtmName = product.CtmName,
                   OrderPath = product.OrderPath,
                   ProductName = product.OrderProductName,
                   Remark = product.Remark + outProcessRecord.OutRemark,
                   OrderBarcode = orderBag.BagCode,
                   ProductId = product.OrderProductId,
                   FacthTime = orderBag.FetchTime,
                   FactoryId = shop.OutShopId,
                   FactoryName = shop.OutShopName,
                   IntStatus = outProcessRecord.Status,
                   IntStutas2 = outProcessRecord.Status2,
                   OrderId = orderBag.OrderBagId,
                   OrderName = orderBag.BagName,
                   OutProcessRecordId = outProcessRecord.OutProcessRecordId,
                   PageNum = product.PageNum,
                   StartProductTime = outProcessRecord.StartProductTime,
                   ProduceRemark = outProcessRecord.Remark,
                   ProductCode = product.OrderProductCode,
                   ProductNum = product.productNum,
                   ShopId = orderBag.Shop.ShopId,
                   ShopName = orderBag.Shop.shopName,
                   Size = product.Width + "X" + product.Height,
                   tb_ProductName = productType.ProductName,

               };
                switch (newView.IntStatus)
                {
                    case (int)OutProcessRecordStatus.Outing:
                        {
                            switch (newView.IntStutas2)
                            {
                                case (int)OutProcessRecordStatus2.Doing:
                                    newView.Status = "正在生产";
                                    break;
                                case (int)OutProcessRecordStatus2.Error:
                                    newView.Status = "暂停生产";
                                    break;
                                case (int)OutProcessRecordStatus2.Complete:
                                    newView.Status = "生产完成";
                                    break;
                                default:
                                    newView.Status = "等待接收";
                                    break;
                            }
                        }
                        break;
                    case (int)OutProcessRecordStatus.InGrid:
                        {
                            newView.Status = "未打印(在格子中)";
                        }
                        break;
                    case (int)OutProcessRecordStatus.OutWaitFenDui:
                        {
                            newView.Status = "未打印(等待分堆)";
                        }

                        break;
                }
                productType.Tb_OutShop_ProductCategory.Load();
                Tb_OutShop_ProductCategory pc = productType.Tb_OutShop_ProductCategory.Where(p => p.OutShopId == shop.OutShopId).FirstOrDefault();
                if (pc != null)
                {
                    newView.TimeSpan = pc.NeedTime;
                }
                else
                {
                    newView.TimeSpan = 0;
                }
                if (outProcessRecord.StartProductTime != null)
                {
                    newView.PreCompleteDateTime = ((DateTime)outProcessRecord.StartProductTime).AddHours(newView.TimeSpan);
                }
                else
                {
                    newView.PreCompleteDateTime = DateTime.Now.AddHours(newView.TimeSpan);
                }


            }
            return newView;
        }

        public ProduceView GetProduceViewByOrderCode(string orderBarcode)
        {
            ProduceView newView = new ProduceView();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                int Status = (int)OutProcessRecordStatus.Fetched;
                int Status2 = (int)OutProcessRecordStatus2.Complete;
                int OrderStatus = (int)OrderInfoStatus.Doing;
                //orderInfo order = db.orderInfo.FirstOrDefault(p => p.photoInfo.Count(ph => ph.PhotoBarCode == photoBarCode) > 0);
                OutProcessRecord outProcessRecord = db.OutProcessRecord.FirstOrDefault(p => p.orderInfo.OrderProductCode == orderBarcode);


                if (!outProcessRecord.orderInfoReference.IsLoaded)
                {
                    outProcessRecord.orderInfoReference.Load();
                }
                orderInfo product = outProcessRecord.orderInfo;
                product.Tb_ProductReference.Load();
                Tb_Product productType = product.Tb_Product;
                //newView.Product = new OrderInfo_Info() { ProductId = product.OrderProductId, CompleteTime = product.CompleteTime, CtmName = product.CtmName, Height = product.Height, Width = product.Width, OrderProductCode = product.OrderProductCode, Remark = product.Remark, OrderProductName = product.OrderProductName, PageNum = product.PageNum, OrderPath = product.OrderPath };
                //newView.ProductType = new Tb_Product_Info() { ProuctId = productType.ProuctId, ProductName = productType.ProductName };

                if (!product.Tb_OrderBagReference.IsLoaded)
                {
                    product.Tb_OrderBagReference.Load();
                }
                Tb_OrderBag orderBag = product.Tb_OrderBag;
                orderBag.ShopReference.Load();
                //newView.Order = new Tb_OrderBag_Info() { BagCode = orderBag.BagCode, BagName = orderBag.BagName, OrderBagId = orderBag.OrderBagId, ShopID = orderBag.Shop.ShopId, ShopName = orderBag.Shop.shopName, FetchTime = orderBag.FetchTime };
                //newView.ProduceInfo = new OutProcessRecord_Info() { CreateTime = list.CreateTime, EndProductTime = list.EndProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.EndProductTime, StartProductTime = list.StartProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.StartProductTime, Status = list.Status, Status2 = list.Status2, OutRemark = list.OutRemark, Remark = list.Remark };
                outProcessRecord.OutProcessShopReference.Load();
                OutProcessShop shop = outProcessRecord.OutProcessShop;
                //newView.Factory = new OutProcessShop_Info() { OutShopId = shop.OutShopId, OutShopName = shop.OutShopName };
                newView = new ProduceView()
                {
                    CreateTime = outProcessRecord.CreateTime,
                    EndProductTime = outProcessRecord.EndProductTime,
                    CtmName = product.CtmName,
                    OrderPath = product.OrderPath,
                    ProductName = product.OrderProductName,
                    Remark = product.Remark + outProcessRecord.OutRemark,
                    OrderBarcode = orderBag.BagCode,
                    ProductId = product.OrderProductId,
                    FacthTime = orderBag.FetchTime,
                    FactoryId = shop.OutShopId,
                    FactoryName = shop.OutShopName,
                    IntStatus = outProcessRecord.Status,
                    IntStutas2 = outProcessRecord.Status2,
                    OrderId = orderBag.OrderBagId,
                    OrderName = orderBag.BagName,
                    OutProcessRecordId = outProcessRecord.OutProcessRecordId,
                    PageNum = product.PageNum,
                    StartProductTime = outProcessRecord.StartProductTime,
                    ProduceRemark = outProcessRecord.Remark,
                    ProductCode = product.OrderProductCode,
                    ProductNum = product.productNum,
                    ShopId = orderBag.Shop.ShopId,
                    ShopName = orderBag.Shop.shopName,
                    Size = product.Width + "X" + product.Height,
                    tb_ProductName = productType.ProductName,

                };
                switch (newView.IntStatus)
                {
                    case (int)OutProcessRecordStatus.Outing:
                        {
                            switch (newView.IntStutas2)
                            {
                                case (int)OutProcessRecordStatus2.Doing:
                                    newView.Status = "正在生产";
                                    break;
                                case (int)OutProcessRecordStatus2.Error:
                                    newView.Status = "暂停生产";
                                    break;
                                case (int)OutProcessRecordStatus2.Complete:
                                    newView.Status = "生产完成";
                                    break;
                                default:
                                    newView.Status = "等待接收";
                                    break;
                            }
                        }
                        break;
                    case (int)OutProcessRecordStatus.InGrid:
                        {
                            newView.Status = "未打印(在格子中)";
                        }
                        break;
                    case (int)OutProcessRecordStatus.OutWaitFenDui:
                        {
                            newView.Status = "未打印(等待分堆)";
                        }

                        break;
                }
                productType.Tb_OutShop_ProductCategory.Load();
                Tb_OutShop_ProductCategory pc = productType.Tb_OutShop_ProductCategory.Where(p => p.OutShopId == shop.OutShopId).FirstOrDefault();
                if (pc != null)
                {
                    newView.TimeSpan = pc.NeedTime;
                }
                else
                {
                    newView.TimeSpan = 0;
                }
                if (outProcessRecord.StartProductTime != null)
                {
                    newView.PreCompleteDateTime = ((DateTime)outProcessRecord.StartProductTime).AddHours(newView.TimeSpan);
                }
                else
                {
                    newView.PreCompleteDateTime = DateTime.Now.AddHours(newView.TimeSpan);
                }


            }
            return newView;
        }
    }
}
